package fairygui.editor.dialogs
{
   import fairygui.GButton;
   import fairygui.GComponent;
   import fairygui.GLoader;
   import fairygui.UIPackage;
   import fairygui.editor.EditorWindow;
   import fairygui.editor.WindowBase;
   import fairygui.editor.extui.Icons;
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.gui.EUIProject;
   import fairygui.editor.handlers.CopyHandler;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.zip.ZipArchive;
   import fairygui.editor.utils.zip.ZipFile;
   import fairygui.event.ItemEvent;
   import fairygui.tree.ITreeListener;
   import fairygui.tree.TreeNode;
   import fairygui.tree.TreeView;
   import flash.filesystem.File;
   
   public class ExportPackageDialog extends WindowBase implements ITreeListener
   {
       
      
      private var _treeView:TreeView;
      
      private var _treeItemUrl:String;
      
      private var _exportedSignUrl:String;
      
      private var _copyHandler:CopyHandler;
      
      public function ExportPackageDialog(param1:EditorWindow)
      {
         super();
         _editorWindow = param1;
         this.contentPane = UIPackage.createObject("Builder","ExportPackageDialog").asCom;
         this.centerOn(_editorWindow.groot,true);
         this._treeItemUrl = UIPackage.getItemURL("Basic","TreeItem");
         this._exportedSignUrl = UIPackage.getItemURL("Builder","bullet_red");
         this._treeView = new TreeView(contentPane.getChild("list").asList);
         this._treeView.listener = this;
         contentPane.getChild("ok").addClickListener(__actionHandler);
         contentPane.getChild("cancel").addClickListener(closeEventHandler);
      }
      
      override protected function onShown() : void
      {
         var _loc4_:TreeNode = null;
         var _loc6_:int = 0;
         var _loc7_:String = null;
         var _loc8_:int = 0;
         var _loc9_:int = 0;
         var _loc10_:int = 0;
         var _loc11_:String = null;
         var _loc12_:TreeNode = null;
         var _loc13_:EPackageItem = null;
         var _loc1_:Vector.<EPackageItem> = _editorWindow.mainPanel.libPanel.pkgsPanel.getSelectedItems(true);
         this._copyHandler = new CopyHandler();
         this._copyHandler.copy(_loc1_,null,"/");
         var _loc2_:Object = {};
         var _loc3_:TreeNode = new TreeNode(true);
         _loc3_.data = "Assets";
         _loc2_["/"] = _loc3_;
         var _loc5_:Vector.<String> = this._copyHandler.targetPaths;
         for each(_loc7_ in _loc5_)
         {
            if(!_loc2_[_loc7_])
            {
               _loc4_ = _loc3_;
               _loc9_ = _loc7_.length;
               _loc10_ = 1;
               _loc6_ = 1;
               while(_loc6_ < _loc9_)
               {
                  if(_loc7_.charAt(_loc6_) == "/")
                  {
                     _loc11_ = _loc7_.substring(0,_loc6_ + 1);
                     _loc12_ = _loc2_[_loc11_];
                     if(!_loc12_)
                     {
                        _loc12_ = new TreeNode(true);
                        _loc12_.data = _loc11_.substring(_loc10_,_loc6_);
                        _loc2_[_loc11_] = _loc12_;
                        _loc4_.addChild(_loc12_);
                     }
                     _loc4_ = _loc12_;
                     _loc10_ = _loc6_ + 1;
                  }
                  _loc6_++;
               }
               continue;
            }
         }
         _loc1_ = this._copyHandler.sourceItems;
         _loc8_ = _loc1_.length;
         _loc6_ = 0;
         while(_loc6_ < _loc8_)
         {
            _loc13_ = _loc1_[_loc6_];
            if(_loc13_.type != EPackageItem.FOLDER)
            {
               _loc4_ = _loc2_[_loc5_[_loc6_]];
               _loc12_ = new TreeNode(false);
               _loc12_.data = _loc13_;
               _loc4_.addChild(_loc12_);
            }
            _loc6_++;
         }
         this._treeView.root.removeChildren();
         this._treeView.root.addChild(_loc3_);
         this._treeView.expandAll(this._treeView.root);
      }
      
      override public function actionHandler() : void
      {
         UtilsFile.browseForSave("FairyGUI package",function(param1:File):void
         {
            if(!param1.exists && (!param1.extension || param1.extension.toLowerCase() != "fairypackage"))
            {
               doExport(new File(param1.nativePath + ".fairypackage"));
            }
            else
            {
               doExport(param1);
            }
         });
      }
      
      private function doExport(param1:File) : void
      {
         var proj:EUIProject = null;
         var pkg:EUIPackage = null;
         var zip:ZipArchive = null;
         var folder:File = null;
         var targetFile:File = param1;
         var tempFolder:File = File.createTempDirectory();
         try
         {
            EUIProject.createNew(tempFolder.nativePath,"tempProject",_editorWindow.project.type);
            proj = new EUIProject(null);
            proj.open(tempFolder.resolvePath("tempProject.fairy"));
            pkg = proj.createPackage("Dummy");
            _editorWindow.cursorManager.setWaitCursor(true);
            this._copyHandler.paste(pkg,CopyHandler.REPLACE);
            _editorWindow.cursorManager.setWaitCursor(false);
            zip = new ZipArchive(null,"GBK");
            folder = new File(pkg.basePath);
            this.addToZip(zip,folder,"");
            UtilsFile.saveBytes(targetFile,zip.output());
            proj.close();
            this.hide();
            return;
         }
         catch(err:Error)
         {
            _editorWindow.alertError(err);
            return;
         }
         finally
         {
            tempFolder.deleteDirectory(true);
         }
      }
      
      private function addToZip(param1:ZipArchive, param2:File, param3:String) : void
      {
         var _loc5_:File = null;
         var _loc6_:ZipFile = null;
         var _loc4_:Array = param2.getDirectoryListing();
         for each(_loc5_ in _loc4_)
         {
            if(_loc5_.isDirectory)
            {
               _loc6_ = new ZipFile(param3 + _loc5_.name + "/");
               param1.addFile(_loc6_);
               this.addToZip(param1,_loc5_,_loc6_.name);
            }
            else
            {
               param1.addFileFromBytes(param3 + _loc5_.name,UtilsFile.loadBytes(_loc5_));
            }
         }
      }
      
      public function treeNodeCreateCell(param1:TreeNode) : GComponent
      {
         return this._treeView.list.getFromPool(this._treeItemUrl).asCom;
      }
      
      public function treeNodeRender(param1:TreeNode, param2:GComponent) : void
      {
         var _loc5_:EPackageItem = null;
         var _loc3_:GButton = param2.asButton;
         var _loc4_:GLoader = param2.getChild("sign") as GLoader;
         if(param1.isFolder)
         {
            _loc3_.title = String(param1.data);
            _loc3_.icon = Icons.all.folder;
            _loc4_.url = null;
         }
         else
         {
            _loc5_ = param1.data as EPackageItem;
            _loc3_.title = _loc5_.name;
            _loc3_.icon = Icons.all[_loc5_.type];
            if(_loc5_.exported)
            {
               _loc4_.url = this._exportedSignUrl;
            }
            else
            {
               _loc4_.url = null;
            }
         }
      }
      
      public function treeNodeWillExpand(param1:TreeNode, param2:Boolean) : void
      {
      }
      
      public function treeNodeClick(param1:TreeNode, param2:ItemEvent) : void
      {
         if(param2.clickCount == 2)
         {
            param1.expanded = !param1.expanded;
         }
      }
   }
}
